layui.use(['form','cookie'], function () {
    let $ = layui.jquery,form = layui.form,layer = layui.layer;

    // 验证码
    draw('#canvas');
    $("#canvas").on('click', function () {
        draw('#canvas');
    })

    // 登录过期的时候，跳出ifram框架
    if (top.location !== self.location) {
        top.location = self.location;
    }

    // // 粒子线条背景
    // $(document).ready(function () {
    //     $('.layui-container').particleground({
    //         dotColor: '#5cbdaa',
    //         lineColor: '#5cbdaa'
    //     });
    // });

    // 进行登录操作
    form.on('submit(login)', function (data) {
        data = data.field;

        if ($.trim(data.username) === '' || $.trim(data.username).length <= 0) {
            layer.msg("用户名不能为空");
            return false;
        }
        if ($.trim(data.password) === '' || $.trim(data.password).length <= 0) {
            layer.msg("密码不能为空");
            return false;
        }
        if (data.captcha.length <= 0) {
            layer.msg("验证码不能为空");
            return false;
        }
        if (data.captcha.toUpperCase() !== $('#canvas').html()) {
            layer.msg("请输入正确验证码");
            return false;
        }

        postAjax("/api/user/login", data, function (res) {
            if(res.code === 0){
                //保存loginToken
                window.sessionStorage.setItem('Authorization', res.data);
                // 获取用户信息
                getAjax(true, "/api/user/current",null,function (res) {
                    if(res.code === 0){
                        $.cookie('login-user', JSON.stringify(res.data));
                    }
                    //重定向跳转首页
                    layer.msg('登录成功',{time: 1000}, function () {
                        window.location = '/index.html';
                    });
                });
            }else if(res.code === -300) {
                layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: true,
                    area: '300px;',
                    shade: 0.8,
                    id: 'ACCOUNTLOCK', //设定一个id，防止重复弹出
                    moveType: 1, //拖拽模式，0或者1
                    content: '<div style="padding: 20px;text-align:center; line-height: 22px; background-color: #393d49; color: #fff; font-weight: 300;">' +
                        '<p style="font-weight: bold;font-size: 15px;margin: 10px">您的账号已被冻结</p>'+
                        '<p style="font-weight: bold;font-size: 15px;margin: 10px">请关注公众号后继续使用'+
                        '<div><img width="150px" height="150px" src="../../images/qrcode.jpg"/></div>'+
                        '<p style="font-size: 10px;margin: 10px">遇到任何问题可添加QQ315200140反馈</p>'+
                        '</div>'
                });
            }else {
                layer.msg(res.msg);
            }
        });

        return false;
    });
    $("#register").on('click', function () {
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: true,
            area: '800px;',
            shade: 0.8,
            id: 'REGISTERWIN', //设定一个id，防止重复弹出
            moveType: 1, //拖拽模式，0或者1
            content: '<div style="padding: 50px;text-align:center; line-height: 22px; background-color: #393d49; color: #fff; font-weight: 300;">' +
                '<p style="font-weight: bold;font-size: 25px;margin: 20px">扫码关注公众号</p>'+
                '<p style="font-weight: bold;font-size: 25px;margin: 20px">回复“<span style="color: red">注册</span>”，即可免费获取账号</p>'+
                '<img width="200px" height="200px" src="../../images/qrcode.jpg"/>'+
                '<p style="font-size: 15px;margin: 20px">遇到任何问题可添加QQ315200140反馈</p>'+
                '</div>'
        });
    });
    $("#forgetPassword").on('click', function () {
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: true,
            area: '800px;',
            shade: 0.8,
            id: 'REGISTERWIN', //设定一个id，防止重复弹出
            moveType: 1, //拖拽模式，0或者1
            content: '<div style="padding: 50px;text-align:center; line-height: 22px; background-color: #393d49; color: #fff; font-weight: 300;">' +
                '<p style="font-weight: bold;font-size: 25px;margin: 20px">扫码关注公众号</p>'+
                '<p style="font-weight: bold;font-size: 25px;margin: 20px">发送“<span style="color: red">重置密码：新密码</span>”，即可重置密码</p>'+
                '<img width="200px" height="200px" src="../../images/qrcode.jpg"/>'+
                '<p style="font-size: 15px;margin: 20px">遇到任何问题可添加QQ315200140反馈</p>'+
                '</div>'
        });
    });
});


function draw(codeID) {
    var captcha = '';
    var verificationCodeArr = [];
    var canvas_width = $(codeID).width();
    var canvas_height = $(codeID).height();
    var canvas = $(codeID)[0];//获取到canvas的对象，演员
    var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
    canvas.width = canvas_width;
    canvas.height = canvas_height;
    var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
    var aCode = sCode.split(",");
    var aLength = aCode.length;//获取到数组的长度

    for (var i = 0; i <= 3; i++) {
        var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
        var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
        var txt = aCode[j];//得到随机的一个内容
        verificationCodeArr[i] = txt.toLowerCase();
        var x = 10 + i * 20;//文字在canvas上的x坐标
        var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
        context.font = "bold 23px 微软雅黑";

        context.translate(x, y);
        context.rotate(deg);

        context.fillStyle = randomColor();
        context.fillText(txt, 0, 0);

        context.rotate(-deg);
        context.translate(-x, -y);
        captcha += txt;
    }
    for (var i = 0; i <= 5; i++) { //验证码上显示线条
        context.strokeStyle = randomColor();
        context.beginPath();
        context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.stroke();
    }
    for (var i = 0; i <= 30; i++) { //验证码上显示小点
        context.strokeStyle = randomColor();
        context.beginPath();
        var x = Math.random() * canvas_width;
        var y = Math.random() * canvas_height;
        context.moveTo(x, y);
        context.lineTo(x + 1, y + 1);
        context.stroke();
    }
    $(codeID).html(captcha)
}

function randomColor() {//得到随机的颜色值
    var r = Math.floor(Math.random() * 256);
    var g = Math.floor(Math.random() * 256);
    var b = Math.floor(Math.random() * 256);
    return "rgb(" + r + "," + g + "," + b + ")";
}